package com.webwork.lims.stock.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.gson.Gson;
import com.webwork.lims.common.controller.SuperCtrl;
import com.webwork.lims.stock.dataobject.Stock;
import com.webwork.lims.stock.service.StockService;
import com.webwork.lims.util.Paging;
import com.webwork.lims.util.StrManager;

@Controller
@RequestMapping(value = "/baseInfo")
public class StockController extends SuperCtrl {

	@Autowired
	private StockService stockSve;
	
	@RequestMapping(value = "/stocks")
	@Override
	public String mainPage() {
		return "stocks";
	}
	
	@RequestMapping(value = "queryStockList")
	@ResponseBody
	public ResponseEntity<String> queryList(
			@RequestParam(value = "page") int page,
			@RequestParam(value = "rows") int rows,
			@RequestParam(required = false, value = "cmd") String cmdStr)
					throws Exception {
		Stock queryObj = new Stock();
		if (!StrManager.isEmpty(cmdStr)) {
			queryObj = new Gson().fromJson(cmdStr, Stock.class);
			queryObj.setSku(queryObj.getSku());
		}
		HttpHeaders responseHeaders = new HttpHeaders();
		responseHeaders.add("Content-Type", "text/html; charset=utf-8");
		Map<String, Object> ret = new HashMap<String, Object>();
	
		ret.put("total", stockSve.count(queryObj));
		ret.put("rows", stockSve.query(queryObj, new Paging(page, rows)));
		
		return new ResponseEntity<String>(new Gson().toJson(ret).toString(), responseHeaders, HttpStatus.CREATED);
	}
	
}
